﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Permission.Interface;
using Microhard.Core.DBUtility;
using Microhard.Core;

namespace Microhard.Permission_MembershipImpl
{
    public class DataPermissionImpl : IDataPermission
    {
        #region IRepository<DataPermission> Members

        public ExecutedResult Add(Permission.Models.DataPermission model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            MicroHard_DataPermission op = model.ToEntity();
            op.ID = Guid.NewGuid();
            op.CreateTime = DateTime.Now;
            op.LastModifiedTime = DateTime.Now;

            db.MicroHard_DataPermissions.InsertOnSubmit(op);
            try
            {
                db.SubmitChanges();
                rtn.Scope.IsSysManage = true;
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("DataPermissionImpl.Add", ex.Message);
                Log.Error("DataPermissionImpl.Add", ex);
            }
            return rtn;

        }

        public ExecutedResult Update(Permission.Models.DataPermission model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var list = db.MicroHard_DataPermissions.SingleOrDefault(n => n.ID == model.ID);

            if (list != null)
            {
                list.OperatorID = model.OperatorID;
                list.LastModifiedTime = DateTime.Now;

                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    rtn.ErrorMsg.Add("DataPermissionImpl.Update", "修改失败，请联系系统管理员");
                    Log.Error("DataPermissionImpl.Update", ex);
                }

            }

            return rtn;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids">DataID</param>
        /// <returns></returns>
        public ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            if (ids == null || ids.Count == 0)
                return result;
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var toDeltes = (from b in db.MicroHard_DataPermissions
                            where ids.Contains(b.DataID.Value)
                            select b).ToList();
            foreach (var d in toDeltes)
            {
                db.MicroHard_DataPermissions.DeleteAllOnSubmit(toDeltes);
            }
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("DataPermissionImpl.Delete", ex.Message);
            }
            return result;
        }

        public ExecutedResult Delete(Permission.Models.DataPermission model)
        {
            throw new NotImplementedException();
        }

        public Permission.Models.DataPermission GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Permission.Models.DataPermission GetModelByModel(Permission.Models.DataPermission model)
        {
            throw new NotImplementedException();
        }

        public List<Permission.Models.DataPermission> Query(Permission.Models.DataPermission Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Permission.Models.DataPermission> Query(Permission.Models.DataPermission Model)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IRepository<DataPermission> Members


        public ScopeResult scope
        {
            set;
            get;
        }

        #endregion
    }
}
